// pages/cart/cart.js
import { getsetting, getChooseAddress, opensetting } from '../../utils/settings'
import { async } from '../../lib/runtime/runtime'
import request from '../../request/index'
Page({
  data: {
    address: {},//本地存储中地址值
    cart: [],//本地存储中的商品信息数据
    allchecked: true,//全选按钮的状态
    totalprice: 0,//总价格
    totalnum: 0//总数量

  },
  //onShow：页面显示或从后台跳回小程序时显示此页面时触发，从跳转页面返回时触发，不能传递参数
  onShow() {
    const address = wx.getStorageSync('address');
    const cart = wx.getStorageSync('cart');
    //const allchecked = cart.every(v => v.ischecked)
    //every是一个数组方法,会遍历数组,会接受一个回调函数,那么, 每一个回调函数都返回true 那么every方法的返回值为true
    //只要有一个回调函数返回了false 那么不在循环执行,则直接返回false
    this.setCart(cart)//调用setCart方法
    this.setData({
      address
    })
  },

  async handleChooseAddress() {
    try {
      const res1 = await getsetting()
      const scopeAddress = res1.authSetting["scope.address"];
      if (scopeAddress === true || scopeAddress === undefined) {//判断权限是否打开过,如果点击过确定或者没有打开过,则直接调用获取收货信息窗口
        const address = await getChooseAddress()
        address.all = address.provinceName + address.cityName + address.countyName + address.detailInfo;
        wx.setStorageSync('address', address);
        console.log(address)
      } else {
        await opensetting()//诱导用户打开权限,如果成功了,则获取收货信息权限
        const address = await getChooseAddress()
        address.all = address.provinceName + address.cityName + address.countyName + address.detailInfo;
        wx.setStorageSync('address', address);
        console.log(address)
      }
    } catch (error) {
      console.log(error)
    }

  },
  handlechange(e) {
    const goods_id = e.currentTarget.dataset.id
    let { cart } = this.data
    let index = cart.findIndex(v => v.goods_id === goods_id)
    cart[index].ischecked = !cart[index].ischecked
    wx.setStorageSync('cart', cart);
    this.setCart(cart)
  },
  //设置复选框状态，重新计算 总价格 和总数量 和全选框状态
  setCart(cart) {
    let totalprice = 0
    let totalnum = 0
    let allchecked = true
    cart.forEach(v => {
      if (v.ischecked) {
        totalprice += v.goods_price * v.num
        totalnum += v.num
      } else {
        allchecked = false
        totalnum = 0
        totalprice = 0
      }
    })
    //判断数组是否为空
    allchecked = cart.length != 0 ? allchecked : false
    this.setData({
      cart,
      totalprice,
      totalnum,
      allchecked: allchecked
    })
  },
  //设置全选框状态改变,同时调用处理总价格和总数量的事件方法
  handleallchange() {
    let { cart, allchecked } = this.data
    allchecked = !allchecked
    cart.forEach(v => v.ischecked = allchecked)
    this.setCart(cart)
  },
  //点击+  // - 实现商品数量增加和减少
  handleItemNumEdit(e) {
    let { cart } = this.data
    const goods_id = e.currentTarget.dataset.id//获取点击商品的id
    const operation = e.target.dataset.operation//获取自定义属性值
    if (operation === -1) {
      const index = cart.findIndex(v => v.goods_id === goods_id)
      cart[index].num = cart[index].num + operation
      if (cart[index].num === 0) {//商品删除
        const res = wx.showModal({ content: '确定要删除吗' });
        if (res.confirm) {
          let { cart } = this.data
          cart.splice(index, 1)
          this.setCart(cart)
        }
      }
    } else {
      const index = cart.findIndex(v => v.goods_id === goods_id)
      cart[index].num = cart[index].num + operation
    }
    this.setCart(cart)
  },
  //结算
  pay() {
    const { address, totalnum } = this.data
    if (!address.userName) {
      wx.showToast({
        title: '请先选择收货地址',
        icon: 'none',

      });
      return;
    }
    if (totalnum === 0) {
      wx.showToast({
        title: '请先选择商品',
        icon: 'none',
      });
      return;
    }
    wx.navigateTo({
      url: '/pages/pay/pay',
    });
  }

})